package com.red.wood.common.ip;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public class IPLocation {
	/**
     * 国家
     */
    private String country;
    /**
     * 区域 - 省份 + 城市
     */
    private String area;

    public IPLocation() {
        country = area = "";
    }

    public synchronized IPLocation getCopy() {
        IPLocation ret = new IPLocation();
        ret.country = country;
        ret.area = area;
        return ret;
    }

    public String getCountry() {
        return country;
    }

    public String getCity() {
        String city = "";
        if(country != null){
            String[] array = country.split("省");
            if(array != null && array.length > 1){
                city =  array[1];
            } else {
                city = country;
            }
            if(city.length() > 3){
                city = city.replace("内蒙古", "");
            }
        }
        return city;
    }
    
    public String getProvince() {
    	String province = "";
    	try {
        	String provinceN = "(?<province>[^省]+省|.+自治区|[^市]+市)";
        	Matcher m = Pattern.compile(provinceN).matcher(country);
    		while (m.find()) {
    			province = m.group("province");
    			if (province != null) {
    				province = province.replace("省", "").replace("自治区", "").replace("市", "");
    				break;
    			}
    		}
    		if(StringUtils.startsWith(province, "内蒙古")) {
            	province = "内蒙古";
            }
    	}catch(Exception e) {
    	}
        return province;
    }
    

    public void setCountry(String country) {
        this.country = country;
    }

    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        //如果为局域网，纯真IP地址库的地区会显示CZ88.NET,这里把它去掉
        if(area.trim().equals("CZ88.NET")){
            this.area="本机或本网络";
        }else{
            this.area = area;
        }
    }
}
